#!/usr/bin/env python
# encoding: utf-8
"""
utils.py

This is a utility that sets up some sample data for
useing the database without having to rebuild the users and groups
when the model is changed.

buildUsers()
	Sets up sample users
	
	>>> from synchronicity.utils.utils import buildUsers
	>>> buildUsers()

Created by Zach Seifts on 2007-01-27.
"""

def buildProject():
	from dev.synchronicity.models import Project
	from django.contrib.auth.models import User
	from datetime import datetime
	
	client1 = User.objects.get(username='SteveSwanson')
	client2 = User.objects.get(username='TrevorSeifts')
	lead1 = User.objects.get(username='ZachSeifts')
	lead2 = User.objects.get(username='ChrisEmery')
	creator = User.objects.get(username='KenIngle')
	
	print "starting to set up projects"
	p1 = Project(title='Test Project One',
	             overview='This is a project used for testing this application',
	             request_date=datetime.today(),
	             client=client1,
	             lead=lead1,
	             creator=creator,
	             active=True)
	p1.save()
	
	p2 = Project(title='Test Project Two',
	             overview='This is another test project used for testing this application',
	             request_date=datetime.today(),
	             client=client2,
	             lead=lead2,
	             creator=creator,
	             active=False)
	p2.save()
	
	p3 = Project(title='Test Project Three',
	             overview='This is yet another test project (yatp) that is used to test this application',
	             request_date=datetime.today(),
	             client=client1,
	             lead=lead1,
	             creator=creator,
	             active=True)
	p3.save()
	
	p4 = Project(title='Test Project Four',
	             overview='One more test project this is so a user can have more than one project',
	             request_date=datetime.today(),
	             client=client2,
	             lead=lead1,
	             creator=creator,
	             active=True)
	p4.save()
	print "Projects have been setup"


def buildObjective():
    from dev.synchronicity.models import Objective, Project
    from django.contrib.auth.models import User
    
    p1 = Project.objects.get(title='Test Project One')
    p2 = Project.objects.get(title='Test Project Two')
    a1 = User.objects.get(username='ZachSeifts')
    a2 = User.objects.get(username='ChrisEmery')
    
    # Project 1 Objectives
    o1 = Objective(label='Collection of things to do, part one',
                   description='Just a few things to do',
                   project=p1,
                   cost_estimate='2',
                   status='unscheduled',
                   assigned=a1)
    o2 = Objective(label='Collection of things to do, part two',
                   description='Some more things to do after part one is finished',
                   project=p1,
                   cost_estimate='2',
                   status='unscheduled',
                   assigned=a1)
    o3 = Objective(label='Collection of things to do part, three',
                   description='How about now, some more things that need to be done.',
                   project=p1,
                   cost_estimate='2',
                   status='unscheduled',
                   assigned=a2)
    
    o4 = Objective(label='Collection of things to do part, four',
                   description='More stuff to do here.',
                   project=p1,
                   cost_estimate='2',
                   status='scheduled',
                   assigned=a2)
    o1.save()
    o2.save()
    o3.save()
    o4.save()


def buildUsers():
	from django.contrib.auth.models import User
	from django.contrib.auth.models import Group
	
	def buildGroups():
		groups = ('Manager', 'Developer', 'Client')
		
		for group in groups:
			g = Group.objects.create(name=group)
			g.save()
			print "Group: %s has been created" % g
		
		print "Groups have been created"
	
	# A list of users to add.
	users = (
		{'username': 'ZachSeifts',
		 'email':'zach.seifts@asdf.com',
		 'password':'password',
		 'first_name': 'Zach',
		 'last_name': 'Seifts',
		 'group': 'Developer',
		 'is_staff': False},
		{'username': 'ChrisEmery',
		 'email':'chris.emery@asdf.com',
		 'password': 'password',
		 'first_name': 'Chris',
		 'last_name': 'Emery',
		 'group': 'Developer',
		 'is_staff': False},
		{'username': 'KenIngle',
		 'email': 'ken.ingle@asdf.com',
		 'password': 'password',
		 'first_name': 'Ken',
		 'last_name': 'Ingle',
		 'group': 'Manager',
		 'is_staff': True,},
		{'username': 'SteveSwanson',
		 'password': 'password',
		 'email': 'steve.swanson@asdf.com',
		 'first_name': 'Steve',
		 'last_name': 'Swanson',
		 'group': 'Client',
		 'is_staff': False},
		{'username': 'TrevorSeifts',
		 'password': 'password',
		 'email': 'trevor.seifts@asdf.com',
		 'first_name': 'Trevor',
		 'last_name': 'Seifts',
		 'group': 'Client',
		 'is_staff': False},
		) 
	
	print "Setting up the groups"
	buildGroups()
	
	print "Starting to set up some users"
	for user in users:
		acct = User.objects.create_user(username = user['username'],
										email = user['email'],
										password = user['password'])
		acct.is_staff = user['is_staff']
		acct.first_name = user['first_name']
		acct.last_name = user['last_name']
		acct.groups.add(Group.objects.get(name=user['group']))
		acct.save()
		print "User: %s has been created" % user['username']